package com.liujit.standard.basis.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liujit.standard.basis.admin.config.NacosConfig;
import com.liujit.standard.basis.admin.config.annotation.Auth;
import com.liujit.standard.basis.admin.config.annotation.Log;
import com.liujit.standard.basis.admin.domain.params.SysUserParam;
import com.liujit.standard.basis.admin.domain.params.UpdPasswordParam;
import com.liujit.standard.basis.admin.domain.query.SysUserQuery;
import com.liujit.standard.basis.admin.model.SysUser;
import com.liujit.standard.basis.admin.service.SysUserService;
import com.liujit.standard.basis.core.util.CoreUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 系统用户管理
 * @Author LiuJun
 * @Date 2021-03-25 17:12:35
 */
@Api(value = "系统用户管理接口", tags = "系统用户管理接口")
@EnableTransactionManagement
@RestController
@RequestMapping("/admin/sys/user")
public class SysUserController {

    @Resource
    private NacosConfig nacosConfig;

    @Resource
    private SysUserService sysUserService;

    @Auth("sysUser-sel")
    @ApiOperation(value = "查询列表", notes = "查询列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "SysUserQuery")})
    @GetMapping("/getList")
    public List<SysUser> getList(SysUserQuery query) {
        CoreUtil.Paging(query, "create_time desc");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.ne("login_name", nacosConfig.getSuperName());
        if (StrUtil.isNotEmpty(query.getLoginName())) {
            wrapper.like("login_name", query.getLoginName());
        }
        if (StrUtil.isNotEmpty(query.getPhoneNumber())) {
            wrapper.like("phone_number", query.getPhoneNumber());
        }
        if (StrUtil.isNotEmpty(query.getNickName())) {
            wrapper.like("nick_name", query.getNickName());
        }
        if (query.getState() != null) {
            wrapper.eq("state", query.getState());
        }
        return sysUserService.list(wrapper);
    }

    @Log
    @Auth("sysUser-add")
    @ApiOperation(value = "添加", notes = "添加")
    @ApiImplicitParams({@ApiImplicitParam(name = "SysUserParam")})
    @PostMapping("/add")
    public String add(@RequestBody SysUserParam param) {
        SysUser sysUser = CoreUtil.modelMapper().map(param, SysUser.class);
        return sysUserService.add(sysUser, param.getRoleIds());
    }

    @Log
    @Auth("sysUser-upd")
    @ApiOperation(value = "修改", notes = "修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id")})
    @PostMapping("/upd")
    public Boolean upd(@RequestBody SysUserParam param) {
        SysUser sysUser = CoreUtil.modelMapper().map(param, SysUser.class);
        return sysUserService.upd(sysUser, param.getRoleIds());
    }

    @Log
    @Auth("sysUser-sel")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id")})
    @GetMapping("/get")
    public SysUser get(Long id) {
        return sysUserService.getById(id);
    }

    @Log
    @Auth("sysUser-del")
    @ApiOperation(value = "删除/批量删除", notes = "删除/批量删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = ",分割的id")})
    @PostMapping("/del")
    public Boolean del(@RequestBody JSONObject param) {
        String id = param.getString("id");
        return sysUserService.del(id);
    }

    @Log
    @Auth("sysUser-updState")
    @ApiOperation(value = "修改用户状态", notes = "修改用户状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "SysUser")})
    @PostMapping("/updState")
    public Boolean updState(@RequestBody SysUser param) {
        return sysUserService.updateById(param);
    }

    @Log
    @Auth("sysUser-updRole")
    @ApiOperation(value = "修改用户状态", notes = "修改用户状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "SysUser")})
    @PostMapping("/updRole")
    public Boolean updRole(@RequestBody SysUserParam param) {
        return sysUserService.updRole(param.getId(), param.getRoleIds());
    }

    @Log
    @ApiOperation(value = "校验密码", notes = "校验密码")
    @ApiImplicitParams({@ApiImplicitParam(name = "password", value = "密码")})
    @PostMapping("/checkPassword")
    public Boolean checkPassword(String password) {
        return sysUserService.checkPassword(password);
    }

    @Log
    @ApiOperation(value = "修改密码", notes = "修改密码")
    @ApiImplicitParams({@ApiImplicitParam(name = "UpdPasswordParam")})
    @PostMapping("/updPassword")
    public Boolean updPassword(@RequestBody UpdPasswordParam param) {
        return sysUserService.updPassword(param);
    }
}
